package com.visualexpress.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.shards.strategy.ShardStrategyFactory;

import com.visualexpress.domain.Site;
import com.visualexpress.shards.HibernateSessionFactory;
import com.visualexpress.shards.ShardConfigurationUtil;
import com.visualexpress.shards.VEShardStrategyFactory;

public class SiteDAO implements IBaseHibernateDAO {
	private static final Log log = LogFactory.getLog(SiteDAO.class);
	private static final String  NAME = "name";
	private static ShardStrategyFactory ssf;
	public void save(Site transientInstance) {
		log.debug("saving Site instance");
		try {
			HibernateSessionFactory.SaveToAllSites(transientInstance);			
			System.out.println(transientInstance.getId());
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	public Site getLocalSite()
	{
		String siteName = ShardConfigurationUtil.getLocalShardConfiguration().getShardSessionFactoryName();
		log.debug("getting the local site :"+siteName);
		return this.findByName(siteName);
	}
	public Site getGlobalSite()
	{
		String siteName = ShardConfigurationUtil.getGlobalShardConfiguration().getShardSessionFactoryName();
		log.debug("getting the  site :"+siteName);
		return this.findByName(siteName);
	}
	public Site findById(String id) {
		log.debug("getting Site instance with id: " + id);
		try {
			Site instance = (Site) getSession().get(
					"com.visualexpress.domain.Site", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	private List findByProperty(String propertyName, Object value) {
		log.debug("finding Site instance with property: " + propertyName
				+ ", value: " + value);
		try {
			getSession().flush();
			String queryString = "from Site as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public Site findByName(String name) {
		log.debug("getting Site instance with name: " + name);
		try {
			List<Site>  sites = this.findByProperty(NAME, name); 
			if(sites==null || sites.size() ==0)
				return null;
			else if (sites.size()>1)
			{
				
				log.warn("get more than one result when qury by name");
				return sites.get(0);
			}
			else 
				return sites.get(0);
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List findAll() {
		log.debug("finding all Site instances");
		try {
			String queryString = "from Site";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public Session getSession() {
	
		
		if(ssf==null)
			ssf = new VEShardStrategyFactory();
		return HibernateSessionFactory.getSession(ssf);
	}
}
